/*
 * RegionCase.cpp
 *
 *  Created on: 2012-9-21
 *      Author: yangentao@gmail.com
 */

#include "Test.h"
#include "../src/yet/Region.h"

#include <sstream>
using namespace std;
using namespace  yet;

class RegionTest: public Region<int>, public testing::Test {

};

TEST_F(RegionTest, a) {

}

string dump(Region<int>& rg) {
	stringstream ss;
	ss << "Length: " << rg.length() << endl;
	ss << "{";
	for (int i = 0; i < rg.length(); ++i) {
		Region<int>::RangeType r = rg.get(i);
		ss << "[" << r.from << ", " << r.to << ")@"<<r.value<<",  ";
	}
	ss << "}" << endl;
	return ss.str();
}

TEST(Region, empty) {
	Region<int> rg;
	EXPECT_TRUE(rg.empty());
}
TEST(Region, add) {
	Region<int> rg;

	//[1, 4)[4, 5)[6,7)
	Range<int> r1(4,5, 1);
	Range<int> r2(1,4, 1);
	Range<int> r3(6,7, 1);
	rg.add(r1);
	rg.add(r2);
	rg.add(r3);

	EXPECT_FALSE(rg.empty());
	EXPECT_EQ(2, rg.length())<<dump(rg);
	EXPECT_TRUE(rg.test());

}
TEST(Region, add2) {
	Region<int> rg(false);

	//[1, 4)[4, 5)[6,7)
	Range<int> r1(4,5, 1);
	Range<int> r2(1,4, 1);
	Range<int> r3(6,7, 1);
	rg.add(r1);
	rg.add(r2);
	rg.add(r3);

	EXPECT_FALSE(rg.empty());
	EXPECT_EQ(3, rg.length())<<dump(rg);
	EXPECT_TRUE(rg.test());

}
TEST(Region, add3) {
	Region<int> rg;
	//[1, 4)[4, 5)[6,7)
	Range<int> r1(4,5, 1);
	Range<int> r2(1,4, 0);
	Range<int> r3(6,7, 1);
	rg.add(r1);
	rg.add(r2);
	rg.add(r3);

	EXPECT_FALSE(rg.empty());
	EXPECT_EQ(3, rg.length())<<dump(rg);
	EXPECT_TRUE(rg.test());
}

TEST(Region, remove1) {
	Region<int> rg;
	rg.add(1, 20, 0);
	rg.add(5, 10, 1);
	rg.remove(2, 8);
	EXPECT_FALSE(rg.empty());
	EXPECT_EQ(3, rg.length())<<dump(rg);
	EXPECT_TRUE(rg.test());
}

TEST(Region, sub1) {
	Region<int> rg;
	rg.add(1, 20, 0);
	rg.add(5, 10, 1);//[1,5)@0, [5,10)@1, [10,20)@0
	rg.remove(2, 8);//[1,2)@0, [8,10)@1, [10,20)@0

	Region<int> s = rg.subRegion(0,10);
	EXPECT_FALSE(rg.empty());
	EXPECT_EQ(3, rg.length())<<dump(rg);
	EXPECT_EQ(2, s.length())<<dump(rg);
	EXPECT_TRUE(rg.test());
}
